﻿using System;
using System.Linq;
using MyFloridaHouse.libSTO;
using MyFloridaHouse.DataAccess;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Types;
using System.Collections.Generic;
using MyFloridaHouse.libSvDTO;

namespace MyFloridaHouse.libServiceBusinessRules
{
    public static class ShapeUogIDs
    {
		/// <summary>
		/// Gets the shape uogIDs as a list of ShapeUogIDDTO objects.
		/// </summary>
		/// <param name="iWktList">The iWKT list.</param>
		/// <param name="iLayerID">The ilayerID.</param>
		/// <param name="iDistrictID">The idistrictID.</param>
		/// <returns>Object: <see cref="ShapeUogidSTO"/></returns>
        public static ShapeUogidSTO GetShapeUogIDs(string iWktList, int iLayerID, int iDistrictID)
        {
            ShapeUogidSTO results = new ShapeUogidSTO();
            try
            {
                SqlChars sc = new SqlChars(iWktList.ToString());
                SqlGeometry newGeom = SqlGeometry.STGeomFromText(sc, 4326);
                if (!newGeom.STIsValid())
                {
                    throw new Exception("Can't evaluate your lasso, Please try again without crossing back into the lasso");
                }

                results.districtID = iDistrictID;

                DAL_CensusData dal = new DAL_CensusData();
                var queryResults = dal.GetShapeUogIDs(iWktList, iLayerID);


                //TODO: Before implementing table storage based lookups, the stored procedure used by GetShapeUogIDs needs to be changed to only look up shapes on the layer its on, not do the region to block lookup
                //List<ShapeUogIDDTO> blockUogIDList = new List<ShapeUogIDDTO>();
                //if (DAL_Global.IsConfigLayer(iLayerID))
                //{

                //    foreach (var shapeUogID in queryResults)
                //    {
                //        blockUogIDList.AddRange(dal.GetRegionUogIDs(DAL_Global.GetPrefixFromLayerID(iLayerID), shapeUogID.uogID));
                //    }
                //}
                //queryResults = blockUogIDList;

                results.uogidList = queryResults;
                return results;
            }
            catch (Exception ex)
            {
                //todo: Log exception
                results.errorMessage = ex.Message;
                return results;
            }
        }

    }
}
